package cn.puppy.postgres.demo.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.puppy.postgres.demo.dao.db1.AccessDatasource1Dao;
import cn.puppy.postgres.demo.dao.db2.ResultDatasourceDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhanglm(nicepuppy)
 * @createDate 2024/09/01 10:51
 * @package cn.hzy.postgres.demo.service
 * @message service定时任务
 */
@Slf4j
@Service
@EnableScheduling
public class TaskService_BAK {
    /**
     * Aad,
     * Ad,
     * Vad,
     * Vd,
     * Vdaf,
     * Fcad,
     * Mad
     */
    private static final String AAD = "Aad";
//    private static final String AD = "Ad";
    private static final String VAD = "Vad";
//    private static final String VD = "Vd";
//    private static final String VDAF = "Vdaf";
    private static final String FCAD = "FCad";//Fcad
    private static final String MAD = "Mad";//mad
    private static final String QNETAR = "Qnet.ar";//mad

    @Resource
    private AccessDatasource1Dao accessDatasource1Dao;

    @Resource
    private ResultDatasourceDao resultDatasourceDao;

    /**
     * 项目启动立即执行
     * cron表达式：每2分钟执行一次
     */
    @PostConstruct
    @Scheduled(cron = "0 0/2 * * * ?")
    @Transactional(rollbackFor = RuntimeException.class)
    public void execute() {
        log.info("***********************task-begin本类用于多条记录汇总于一条，临时不用了。未添加到gitee**************************");
        log.info("**************************************************************");
        log.info("         shandong luli Steel Co., Ltd.IT DEPT");
        log.info("            Labtory Dept data(光谱仪spectrometer) synchroize");
        log.info("                Auther:nicepuppy(zlm)");
        log.info("                    DATE:2024/08/18");
        log.info("**************************************************************");
        //用于记录更新的记录id
        List<String> itemIdList = new ArrayList<>();
        int updateCount = 0;
        try {

            List<Map<String, Object>> mdbList = accessDatasource1Dao.listMdbTestData(null);
            log.info(String.valueOf(mdbList.size()));
            if (CollectionUtil.isNotEmpty(mdbList)) {
                String samp = "";
//                Spectrometer spectrometer = new Spectrometer();
                for (Map<String, Object> mdbMap : mdbList) {
                    //样品编号
                    String sampleNo = mdbMap.get("SampleNo")== null ?"":mdbMap.get("SampleNo").toString();

                    String element = mdbMap.get("ElementOrChannel")== null ?"":mdbMap.get("ElementOrChannel").toString();
                    Integer count = resultDatasourceDao.countSampleNoAndElement(sampleNo,element);
//                    if (count <= 0) {
//                        int sampleCodeAndElement = resultDatasourceDao.save(mdbMap);
//                        updateCount += sampleCodeAndElement;
//                    }
                    Integer countSamp = resultDatasourceDao.countSampleNo2(sampleNo);
                    if(countSamp <= 0){
                        resultDatasourceDao.save2(mdbMap);
                    }else {
                        resultDatasourceDao.update(mdbMap);
                    }
//                    if(samp.equals("")){
//                        samp = sampleNo;
//                    }else {
//                        if(samp.equals(sampleNo)){
//                            final Method method = spectrometer.getClass().getMethod("set" +element);
//                            try{
//                                method.invoke(spectrometer,mdbMap.get("ContentOrIntensity"));
//                            }catch (Exception ex){
//                                ex.printStackTrace();
//                            }
//                        }
//                    }
                    //不存在该数据，跳过该次循环

                }

                /*
                这个是用来更新Lims数据库的，目前不用，用的时候解开注释即可

                for (Map<String, Object> mdbMap : mdbList) {
                    //样品编号
                    String sampleNo = mdbMap.get("试样编号")== null ?"":mdbMap.get("试样编号").toString();
                    Integer count = resultDatasourceDao.countSampleNo(sampleNo);
                    //不存在该数据，跳过该次循环
                    if (count <= 0) {
                        continue;
                        //存在数据，根据编号更新
                    } else {
                        //更新数据集
                        HashMap<String, Object> condition = new HashMap<>();

                        //Aad
                        BigDecimal Qnetar = mdbMap.get("收到基低位热值") != null ? new BigDecimal(mdbMap.get("收到基低位热值").toString()).setScale(2, RoundingMode.HALF_UP) : null;
                        if (null != Qnetar) {
                            //根据名称跟样品编号查询item表中的id
                            String itemId = resultDatasourceDao.getManufactItemByNameAndTransferCode(TaskService.QNETAR, sampleNo);
                            //数据存在，则更新
                            if (StrUtil.isNotBlank(itemId)) {
                                BigDecimal bigDecimalDoubleValue = BigDecimal.valueOf(4.1816);
                                BigDecimal divide = Qnetar.divide(bigDecimalDoubleValue,3,RoundingMode.HALF_UP);

                                BigDecimal thousand  = divide.multiply(BigDecimal.TEN).multiply(BigDecimal.TEN).multiply(BigDecimal.TEN);
                                BigDecimal result = thousand.setScale(0,RoundingMode.HALF_UP);
                                condition.put("testValue",  result+ "");
                                condition.put("id", Long.parseLong(itemId));
                                //更新Aad
                                resultDatasourceDao.updateManufactItemById(condition);
                                itemIdList.add(itemId);
                            }
                        }
                    }
                }*/
            }else {
                System.out.println("空数据！！！");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.error("出问题了----");
        }
//        log.warn("本次定时任务更新数据的条数：" + itemIdList.size() + "，id集合为：" + itemIdList);
        log.warn("本次定时任务更新数据的条数：" + updateCount );
        log.info("***********************task-end**************************");
        //清空集合
        itemIdList.clear();
    }

}
